﻿Imports EstoqueBLL
Public Class FornecedorCON
    Dim objBLL As New FornecedorBLL()
    Public tipo As String
    Public objforn As New FornecedorBLL
#Region "Cadastrar"
    Private Sub TSBCadastrar_Click(sender As System.Object, e As System.EventArgs) Handles TSBCadastrar.Click
        FornecedorCAD.ShowDialog()
        carregagrid()
    End Sub
#End Region

#Region "Carrega Grid"
    Public Sub carregagrid()
        Dim dt As New DataTable
        dt = objBLL.buscar("", 9)
        DGVDados.DataSource = dt
        DGVDados.Refresh()
    End Sub
#End Region

#Region "ComboBox Filtro"
    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        TXTLocalizar.Clear()
        If (CBFiltro.Text = "Status") Then
            TXTLocalizar.Visible = False
            CBStatus.Visible = True
            CBStatus.SelectedIndex = 0
            CBStatus_SelectedIndexChanged(Nothing, Nothing)
        Else
            TXTLocalizar.Clear()
            TXTLocalizar.Visible = True
            CBStatus.Visible = False
        End If
        If TXTLocalizar.Text = "" Then
            CarregaGrid()
        End If
    End Sub
#End Region

#Region "ComboboxStatus Selectedindex"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        Dim sta As Integer = 0
        If (CBStatus.Text = "Ativo") Then sta = 1
        If (CBStatus.Text = "Inativo") Then sta = 2
        DGVDados.DataSource = objBLL.buscar(sta, 5)
        DGVDados.Refresh()
    End Sub
#End Region

#Region "Localizar no textbox"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Select Case CBFiltro.Text
            Case "Código"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 0)
            Case "Razão Social"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 1)
            Case "Nome Fantasia"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 2)
            Case "CNPJ"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 3)
            Case "Inscr. Estadual"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 4)
            Case "Status"
                DGVDados.DataSource = objBLL.buscar(1, 5)
        End Select
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = objBLL.buscar("", 9)
        End If
        DGVDados.Refresh()
    End Sub

#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region

#Region "Load"

    Private Sub FornecedorCON_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            SendKeys.Send("{TAB}")
        End If
        If (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If
    End Sub

    Private Sub FabricanteCON_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        carregagrid()
        CBFiltro.SelectedIndex = 0
        CBFiltro.Focus()
    End Sub
#End Region

#Region "ComboboxFiltro SelectedIndex"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        TXTLocalizar.Clear()
        If (CBFiltro.Text = "Status") Then
            TXTLocalizar.Visible = False
            CBStatus.Visible = True
            CBStatus.SelectedIndex = 0
        Else
            TXTLocalizar.Clear()
            TXTLocalizar.Visible = True
            CBStatus.Visible = False
        End If
        If TXTLocalizar.Text = "" Then
            carregagrid()
        End If
    End Sub
#End Region

#Region "Keypress Localizar"
    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress, TXTLocalizar.KeyPress
        If CBFiltro.SelectedIndex = 0 Or CBFiltro.SelectedIndex = 3 Or CBFiltro.SelectedIndex = 4 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
    End Sub
#End Region

#Region "Double click do datagrid"
    Private Sub DGVDados_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        Try

            If tipo = "ret" Then
                Dim indice As Integer = -1

                'Percorre O DataGridView Verificando a Linha selecionada
                For Each linha As DataGridViewRow In DGVDados.SelectedRows
                    If (Not linha.Cells("for_codigo") Is Nothing) Then
                        'Guarda A Posição Selecionada
                        indice = linha.Index
                    End If
                Next
                If (indice <> -1) Then
                    If (DGVDados.Rows(indice).Cells("sta_codigo").Value = 1) Then
                        objforn = New FornecedorBLL(Val(DGVDados.Rows(indice).Cells("for_codigo").Value.ToString()))
                        Me.Close()
                    Else
                        Throw New Exception("Fabricante inativo")
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
#End Region

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

#Region "DatagridView Clicar com enter"
    Private Sub DGVDado_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DGVDados.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            DGVDados_CellDoubleClick(Nothing, Nothing)
        End If
    End Sub
#End Region
End Class